<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>发布博文-MyBlog</title>
    <style>
        .content{
            width: 600px;
            height: 200px;
        }
        .error{
            font-size: 12px;
            color: red;
        }
    </style>
    <script src="https://cdn.bootcss.com/jquery/2.1.2/jquery.min.js"></script>
    <script>

        function check(){

            $("#err_blog_title").html('');
            $("#err_blog_content").html('');


            if ($("#blog_title").val() == "") {
                $("#err_blog_title").html('提示：请填写文章标题！');
                $("#blog_title").focus();
                return false;
            }

            if ($("#blog_content").val() == "") {
                $("#err_blog_content").html('提示：请填写文章内容！');
                $("#blog_content").focus();
                return false;
            }


        }

    </script>
</head>
<body>

<?php

include 'inc/conn.php';

include 'inc/checkSesion.php';

include 'inc/admin_header.php';

if ($_GET['act'] === 'adding') {


    $blog_title = $_POST['blog_title'];
    $blog_content = $_POST['blog_content'];

    if (empty($blog_title) || empty($blog_content)) {
        die("<script>alert('提示：内容有误！');history.back();</script>");
    }

    $sqlAdd = "INSERT INTO blog_info (blog_title, blog_content, add_time) VALUES('".htmlspecialchars($blog_title, ENT_QUOTES)."', 
    '".htmlspecialchars($blog_content, ENT_QUOTES)."', ".time().") ";

    $resAdd = mysqli_query($conn, $sqlAdd);

    if (!$resAdd) {
        die("<script>alert('提示：内容有误！');history.back();</script>");
    }

    echo "<script>alert('文章发布成功！'); window.location.href='list.php';</script>";

}

?>

<div>

    <form id="form" action="add_blog.php?act=adding" method="post" onsubmit="return check();">

        <p>
            文章标题：<input type="text" name="blog_title" id="blog_title">
            <label id="err_blog_title" class="error"></label>
        </p>

        <p>
            文章内容：
            <textarea name="blog_content" id="blog_content" class="content"></textarea>
            <label id="err_blog_content" class="error"></label>
        </p>

        <p>
            <button>发布文章</button>
            <button type="button" onclick="window.location.href='list.php';">取消发布</button>
        </p>

    </form>

</div>



</body>
</html>